package day17.homework1;

import lombok.SneakyThrows;
import util.MyUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    @SneakyThrows
    public static void main(String[] args) {
        ServerSocket serverSocket = new ServerSocket(9527);
        while (true) {
            try (Socket socket = serverSocket.accept()) {// 阻塞等待连接)
                while(true) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    // 因为要收数据，所以用输入流接，从socket里面取接
                    String message = reader.readLine();
                    System.out.println("【服务端】收到消息：" + message);
                    if ("byebye".equals(message)) break;
                    String inputMessage = MyUtils.input();
                    writer.write(inputMessage + "\n");
                    writer.flush();// 把缓冲区内容冲出去
                    if(inputMessage.equals("byebye")) break;
                }
            }
        }
    }
}
